package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import org.example.ttms.customize.Set;
import org.example.ttms.customize.Window;
import org.example.ttms.entityClass.train.Trips;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class SetStatusController {

    @FXML
    private ChoiceBox<String> statusChoiceBox;

    @FXML
    private ChoiceBox<Integer> hourChoiceBox;

    @FXML
    private ChoiceBox<Integer> minuteChoiceBox;

    @FXML
    private Button button;

    private String[] statusArray = {"正常", "早点", "晚点"};

    public String[] getStatusArray() {
        return statusArray;
    }

    public void setStatusArray(String[] statusArray) {
        this.statusArray = statusArray;
    }
    public void initialize(){
        Trips trips = Set.trips;
        String status = trips.getStatus();
        //时间choiceBox
        List<Integer> hourList = new ArrayList<>();
        for(int i = 0; i < 24; i++){
            hourList.add(i);
        }
        hourChoiceBox.getItems().addAll(hourList);
        List<Integer> minuteList = new ArrayList<>();
        for(int i = 0; i < 60; i++){
            minuteList.add(i);
        }
        minuteChoiceBox.getItems().addAll(minuteList);
        statusChoiceBox.getItems().addAll(statusArray);
        if(status.equals("正常")){
            statusChoiceBox.getSelectionModel().select(0);
            hourChoiceBox.getSelectionModel().select(0);
            minuteChoiceBox.getSelectionModel().select(0);
        }
        else if(status.contains("早点")){
            statusChoiceBox.getSelectionModel().select(1);
            hourChoiceBox.getSelectionModel().select(Integer.parseInt(status.split("早点|时|分")[1]));
            minuteChoiceBox.getSelectionModel().select(Integer.parseInt(status.split("早点|时|分")[2]));
        }
        else{
            statusChoiceBox.getSelectionModel().select(2);
            hourChoiceBox.getSelectionModel().select(Integer.parseInt(status.split("晚点|时|分")[1]));
            minuteChoiceBox.getSelectionModel().select(Integer.parseInt(status.split("晚点|时|分")[2]));
        }

    }

    @FXML
    void chickOk(ActionEvent event) {
        String status = statusChoiceBox.getValue();
        if(status.equals("早点") || status.equals("晚点")){
            status = status + hourChoiceBox.getValue() + "时" + minuteChoiceBox.getValue() + "分";
        }
        else if(hourChoiceBox.getValue() == 0 && minuteChoiceBox.getValue() == 0){
            status = "正常";
        }
        Trips trips = Set.trips;
        trips.changeStatus(trips, status);
        if(Set.type == 1) {
            TripsTableViewController.observableList.clear();
            TripsTableViewController.observableList.addAll(trips.obtainAllTrips());
        }
        else{
            SearchTripsController.observableList.clear();
            SearchTripsController.observableList.addAll(trips.searchTrips(
                    Set.searchContent, Set.searchType
            ).values());
        }
        Window.stage.close();
    }

}
